package net.kesumu.yushabuserver.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * class for connection with database
 * the original Class from zhang_xz in xjtuse
 * @author Z 2014-12-20
 * 
 */
public class DataSource {

	private static String DRIVER = "com.mysql.jdbc.Driver";
	private String URL = "jdbc:mysql://localhost:3306/yushabu?characterEncoding=utf8";
	private String USER = "yuyuchat";
	private String PASSWORD = "1234";
	public final static String dbName = "yushabu";
//	private String URL = "jdbc:mysql://10.4.26.93:3306/d482a03378c1444d78159e0b4f66c440c?characterEncoding=utf8";
//    private String USER = "u5aV1Ysiv3dM2";
//    private String PASSWORD = "pYiTO184NlLZL";
//    public final static String dbName = "d482a03378c1444d78159e0b4f66c440c";
    private Connection conn;
    
    
    /**
     * 加载jar
     */
    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * 创建连接
     * @return
     */
    public Connection getConnection() {
        conn = null;
        try {
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
//            System.out.println("Success connect to" + URL + " !");
        } catch (SQLException ex) {
            Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
        }
        return conn;
    }
    
    /**
     * 关闭连接
     */
    public void close() {
        try {
            if(conn != null) {
                conn.close();
                conn=null;
            }
//            System.out.println("Success close to" + URL + " !");
        } catch (SQLException ex) {
            Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
    

